package com.shop.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shop.constant.Constants;
import com.shop.entity.LookCartVO;
import com.shop.exception.*;
import com.shop.factory.ObjectFactory;
import com.shop.service.ShoppingCartService;
import com.shop.service.ShoppingService;
import com.shop.service.impl.ShoppingCartServiceImpl;
import com.shop.service.impl.ShoppingServiceImpl;
import com.shop.util.GetMap;
import com.shop.util.PrintJson;
import com.shop.util.ReturnImg;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>Title: ${file_name}</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2020</p>
 * <p>Company: www.baidudu.com</p>
 *
 * 410 系统正在维护中
 * 411 用户不存在
 * 412 用户已存在
 * 413 用户信息修改失败
 * 414 购物车中商品删除失败
 * 415 商品数量修改失败
 * 416 商品状态修改失败
 * 417 购物车清空失败
 *
 * @author andy
 * @version 1.0
 * @date ${date}
 */

@WebServlet("/shoppingcart/*")
public class ShoppingCartController extends HttpServlet {

    private ShoppingCartService shoppingCartService = (ShoppingCartServiceImpl) ObjectFactory.getInstance(Constants.SHOPPING_CART_SERVICE_KEY);

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI();
        if("/shop/shoppingcart/addcart".equals(uri)) {
            addCart(req,resp);
        }else if("/shop/shoppingcart/clear".equals(uri)) {
            clearCart(req,resp);
        }else if("/shop/shoppingcart/changestate".equals(uri)) {
            changeState(req,resp);
        }else if("/shop/shoppingcart/changestate/single".equals(uri)) {
            changeStateSingle(req,resp);
        }else if("/shop/shoppingcart/changenum".equals(uri)) {
            changeNum(req,resp);
        }else if("/shop/shoppingcart/deletegoods".equals(uri)) {
            deleteGoods(req,resp);
        }else if("/shop/shoppingcart/cart/look".equals(uri)) {
            lookCart(req,resp);
        }else if("/shop/shoppingcart/img".equals(uri)) {
            getGoodsImg(req,resp);
        }else if("/shop/shoppingcart/get_total".equals(uri)) {
            getTotal(req,resp);
        }
    }

    private void getTotal(HttpServletRequest req, HttpServletResponse resp) {
        String u_id1 = req.getParameter("u_id");
        int u_id = Integer.parseInt(u_id1);

        Map<String, Object> map = null;
        try {
            double total = shoppingCartService.getTotal(u_id);
            map = GetMap.getMap(200, "总金额返回成功");
            map.put("data",total);
        } catch (DAOException e) {
            map = GetMap.getMap(410, e.getMessage());
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        PrintJson.printJsonObj(resp,map);
    }

    private void getGoodsImg(HttpServletRequest req, HttpServletResponse resp) {
        String gd_id = req.getParameter("gd_id");

        Map<String, Object> map = null;
        try {
           String path = shoppingCartService.getGoodsImg(gd_id);
           ReturnImg.returnImg(resp,path);
           map = GetMap.getMap(200, "用户图片获取成功");
        } catch (DAOException e) {
            map = GetMap.getMap(410, e.getMessage());
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        PrintJson.printJsonObj(resp,map);
    }

    //查看购物车
    private void lookCart(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("u_id");
        int u_id = Integer.parseInt(id);

        Map<String, Object> map = null;
        try {
            List<LookCartVO> list = shoppingCartService.lookCart(u_id);
            map = GetMap.getMap(200, "查看购物车信息成功");
            map.put("data",list);
        } catch (DAOException e) {
            map = GetMap.getMap(410, e.getMessage());
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        PrintJson.printJsonObj(resp,map);
    }

    private void deleteGoods(HttpServletRequest req, HttpServletResponse resp) {
        JSONObject json = PrintJson.paraseJson(req);
        String sc_id1 = json.getString("sc_id");
        int sc_id = Integer.parseInt(sc_id1);
        String sd_id1 = json.getString("sd_id");
        int sd_id = Integer.parseInt(sd_id1);

        Map<String, Object> map = null;
        try {
            int result = shoppingCartService.deleteGoodsById(sc_id,sd_id);
            map = GetMap.getMap(200, "购物车商品删除成功");
        } catch (DAOException e) {
            map = GetMap.getMap(410, e.getMessage());
            System.out.println(e.getMessage());
            e.printStackTrace();
        } catch (DeleteGoodsFromCartFailException e) {
            map = GetMap.getMap(414, e.getMessage());
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        PrintJson.printJsonObj(resp,map);
    }

    private void changeNum(HttpServletRequest req, HttpServletResponse resp) {
        JSONObject json = PrintJson.paraseJson(req);
        String sc_id1 = json.getString("sc_id");
        int sc_id = Integer.parseInt(sc_id1);
        String sd_id1 = json.getString("sd_id");
        int sd_id = Integer.parseInt(sd_id1);
        String sd_num1 = json.getString("sd_num");
        int sd_num = Integer.parseInt(sd_num1);

        Map<String, Object> map = null;
        try {
            int result = shoppingCartService.changeNum(sc_id,sd_id,sd_num);
            map = GetMap.getMap(200, "购物车商品数量修改成功");
        } catch (DAOException e) {
            map = GetMap.getMap(410, e.getMessage());
            System.out.println(e.getMessage());
            e.printStackTrace();
        } catch (ChangeNumFromCartFailException e) {
            map = GetMap.getMap(415, e.getMessage());
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        PrintJson.printJsonObj(resp,map);
    }

    private void changeStateSingle(HttpServletRequest req, HttpServletResponse resp) {
        JSONObject json = PrintJson.paraseJson(req);
        String sc_id1 = json.getString("sc_id");
        int sc_id = Integer.parseInt(sc_id1);
        String sd_id1 = json.getString("sd_id");
        int sd_id = Integer.parseInt(sd_id1);
        String sd_state1 = req.getParameter("sd_state");
        int sd_state = Integer.parseInt(sd_state1);

        Map<String, Object> map = null;
        try {
            int result = shoppingCartService.changeStateSingle(sc_id,sd_id,sd_state);
            map = GetMap.getMap(200, "购物车商品状态修改成功，自选");
            map.put("total",result);
        } catch (DAOException e) {
            map = GetMap.getMap(410, e.getMessage());
            System.out.println(e.getMessage());
            e.printStackTrace();
        } catch (ChangeStateFromCartFailException e) {
            map = GetMap.getMap(416, e.getMessage());
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        PrintJson.printJsonObj(resp,map);
    }

    private void changeState(HttpServletRequest req, HttpServletResponse resp) {
        JSONObject json = PrintJson.paraseJson(req);
        String sc_id1 = json.getString("sc_id");
        int sc_id = Integer.parseInt(sc_id1);
        String sd_id1 = json.getString("sd_id");
        int sd_id = Integer.parseInt(sd_id1);
        String sd_state1 = req.getParameter("sd_state");
        int sd_state = Integer.parseInt(sd_state1);

        Map<String, Object> map = null;
        try {
            int result = shoppingCartService.changeState(sc_id,sd_id,sd_state);
            map = GetMap.getMap(200, "购物车商品状态修改成功");
            map.put("total",result);
        } catch (DAOException e) {
            map = GetMap.getMap(410, e.getMessage());
            System.out.println(e.getMessage());
            e.printStackTrace();
        } catch (ChangeStateFromCartFailException e) {
            map = GetMap.getMap(416, e.getMessage());
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        PrintJson.printJsonObj(resp,map);
    }

    private void clearCart(HttpServletRequest req, HttpServletResponse resp) {
        String sc_id1 = req.getParameter("sc_id");
        int sc_id = Integer.parseInt(sc_id1);

        Map<String, Object> map = null;
        try {
            int result = shoppingCartService.clearCart(sc_id);
            map = GetMap.getMap(200, "购物车已清空");
        } catch (DAOException e) {
            map = GetMap.getMap(410, e.getMessage());
            System.out.println(e.getMessage());
            e.printStackTrace();
        } catch (CartClearFailException e) {
            map = GetMap.getMap(417, e.getMessage());
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        PrintJson.printJsonObj(resp,map);

    }

    private void addCart(HttpServletRequest req, HttpServletResponse resp) {
        String sc_id1 = req.getParameter("sc_id");
        int sc_id = Integer.parseInt(sc_id1);
        String gd_id1 = req.getParameter("gd_id");
        int gd_id = Integer.parseInt(gd_id1);
//        String sd_state1 = req.getParameter("sd_state");
//        int sd_state = Integer.parseInt(sd_state1);
        String sd_num1 = req.getParameter("sd_num");
        int sd_num = Integer.parseInt(sd_num1);

        Map<String, Object> map = null;
        try {
            int result = shoppingCartService.addCart(sc_id,gd_id,sd_num,0);
            map = GetMap.getMap(200, "商品成功添加进购物车");
        } catch (DAOException e) {
            map = GetMap.getMap(410, e.getMessage());
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        PrintJson.printJsonObj(resp,map);
    }
}
